/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.monkey;

import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.refactoring.rename.RenameProcessor;
import com.intellij.util.IncorrectOperationException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ConstantToFieldIntention extends BaseIntention {
    private boolean constant;

    public String getText() {
        return constant ? "Convert Constant to Field" : "Convert Field to Constant";
    }

    public String getFamilyName() {
        return "PurpleMonkeyDishwasher";
    }

    public boolean isAvailable(Project project, Editor editor, PsiFile psifile) {
        PsiElement element = getElement(editor, psifile);
        PsiField psiField = (PsiField) getContainingElement(element, PsiField.class);
        if (psiField != null && psiField.getInitializer() != null) {
            PsiModifierList modifierList = psiField.getModifierList();
            constant = modifierList.hasModifierProperty(PsiModifier.STATIC) &&
                    modifierList.hasModifierProperty(PsiModifier.FINAL);
            return true;
        }
        return false;
    }

    public void invoke(Project project, Editor editor, PsiFile psifile) throws IncorrectOperationException {
        PsiElement element = getElement(editor, psifile);
        PsiField psiField = (PsiField) getContainingElement(element, PsiField.class);
        psiField.getModifierList().setModifierProperty(PsiModifier.STATIC, !constant);
        psiField.getModifierList().setModifierProperty(PsiModifier.FINAL, !constant);
        RenameProcessor renameprocessor = new RenameProcessor(project, psiField, getNewName(psiField.getName()),
                true, true);
        renameprocessor.run();

    }

    public boolean startInWriteAction() {
        return true;
    }

    private String getNewName(String oldName) {
        if ((constant && oldName.matches("[a-z][A-Za-z0-9]+")) ||
                (!constant && oldName.matches("[A-Z0-9_]+"))) {
            return oldName;
        }
        if (constant) {
            StringBuffer sb = new StringBuffer();
            Pattern pattern = Pattern.compile("_([a-z])");
            Matcher matcher = pattern.matcher(oldName.toLowerCase());
            while (matcher.find()) {
                String letter = matcher.group(1);
                matcher.appendReplacement(sb, letter.toUpperCase());
            }
            matcher.appendTail(sb);
            return sb.toString();
        } else {
            return oldName.replaceAll("([a-z])([A-Z])([a-z])", "$1_$2$3").toUpperCase();
        }
    }
}
